// pages/center/order/order.js
var util = require("../../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    OrderList: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '稍等',
    });
    var UserID = getApp().globalData.userId;
    if (UserID == "") {
      wx.showToast({
        title: '请先登录',
      });
      setTimeout(function () {
        wx.navigateBack({
          delta: 1,
        });
      }, 1000);
    } else {
      var params = {
        Param: {
          UserID: UserID,
          PageIndex: 1,
          ShowIndexNum: 50,
          SearchKey: ""
        },
        Action: 570
      };
      var page = this;
      util.reqMedia(params, function (value) {
        wx.hideLoading();
        page.setData({
          OrderList: value.OrderList
        });
        if(value.OrderList==''){
           wx.showToast({
             title: '暂无订单',
           })
        }

      });

    }


  },
  onPay: function (e) {
    
    var orderId = e.target.dataset.id;
    var orderList = this.data.OrderList;
    var orderInfo = null;
    for(let i =0;i<orderList.length;i++){
      if (orderList[i]['xy_OutTradeNo']==orderId){
            orderInfo = orderList[i];
            break;
        }        
    }
    var data = {
      Title: orderInfo.Title,
      Content: orderInfo.Content,
      xy_OutTradeNo: orderInfo.xy_OutTradeNo,
      Price: orderInfo.Price * 100
    };
    var sessionId = getApp().globalData.sessionId;
    wx.request({
      url: getApp().globalData.basePath.miniAppPath + "/wxMa/4",
      data: data,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "JSESSIONID=" + sessionId
      },
      success: function (res) {
     
        if (res.data.code == "41") {
          var orderJson = res.data;
        
          wx.requestPayment({
            timeStamp: orderJson.timeStamp,
            nonceStr: orderJson.nonceStr,
            package: 'prepay_id=' + orderJson.prepayId,
            signType: orderJson.signType,
            paySign: orderJson.sign,
            fail: function (res) {
                
            },
            success: function (res) {
         
              wx.navigateTo({
                url: '/pages/afterPay/afterPay?detail=' + JSON.stringify(page.data.detail),
              });
            }
          });
        }
       
      }
    });
     
  },

 

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '稍等',
    })
     wx.reLaunch({
       url: '.',
     })
     wx.hideLoading();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})